package com.cg;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class CompleteFutureTest01 {

    public static void main(String[] args) throws Exception {
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "执行");
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getOne();
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "执行");
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getTwo();
        });
        CompletableFuture<Integer> result = completableFuture1.thenCombine(completableFuture2, Integer::sum);
        System.out.println(result.get(5,TimeUnit.SECONDS));
//        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
//            return "hello";
//        });
//        CompletableFuture<String> future4 = CompletableFuture.supplyAsync(() -> {
//            return "world";
//        });
//        CompletableFuture<String> f = future3.thenCombine(future4,
//                (x, y) -> x + "-" + y);
//        System.out.println(f.get());

    }

    public static Integer getOne() {
        return 1;
    }

    public static Integer getTwo() {
        return 2;
    }

}
